//
//  TTICommonUtils.m
//  ContentApp
//
//  Created by Jery on 2018/7/16.
//  Copyright © 2018年 翔傲科技. All rights reserved.
//

#import "TTICommonUtils.h"

@implementation TTICommonUtils

void NAV_INIT(UIViewController *vc ,NSString *title, id left ,SEL leftObj, id right, SEL rightObj)
{
    if(left != nil){
        
        if([left isKindOfClass:[UIImage class]])
        {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 40)];
            UIImage *image = (UIImage *)left;
            CGFloat size = image.size.width > image.size.height ? image.size.width : image.size.height;
            size = size>40 ? 40 : size;
            UIImageView *leftImgView = [[UIImageView alloc] initWithFrame:CGRectMake(-5, (40-size)/2, size, size)];
            leftImgView.contentMode = UIViewContentModeCenter;
            leftImgView.image = image;
            [button addSubview:leftImgView];
//            button.backgroundColor = [UIColor redColor];
            [button addTarget:vc.self action:leftObj forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
            vc.navigationItem.leftBarButtonItem = leftBarBtnItem;
        }
        else if ([left isKindOfClass:[NSString class]])
        {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
            UILabel *titleTxt = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
            titleTxt.text = left;
            [titleTxt setFont:FontSize_Button];
            titleTxt.textColor = NavTitle_TextColor;
            titleTxt.textAlignment = NSTextAlignmentLeft;
            [button addSubview:titleTxt];
            
            [button addTarget:vc.self action:leftObj forControlEvents:UIControlEventTouchUpInside];
            
            [titleTxt setBackgroundColor:[UIColor clearColor]];
            [button setBackgroundColor:[UIColor clearColor]];
            UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
            vc.navigationItem.leftBarButtonItem = leftBarBtnItem;
        }
    }
    else
        vc.navigationItem.leftBarButtonItem = nil;
    
    if(right != nil){
        if([right isKindOfClass:[UIImage class]])
        {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220, 10, 30, 40)];
            UIImage *image = (UIImage *)right;
            CGFloat size = image.size.width > image.size.height ? image.size.width : image.size.height;
            size = size>40 ? 40 : size;
            
            UIImageView *rightImgView = [[UIImageView alloc] initWithFrame:CGRectMake((30-size)/2 + 12, (40-size)/2, size, size)];
            rightImgView.contentMode = UIViewContentModeCenter;
            rightImgView.image = image;
            
            [button addSubview:rightImgView];
            [button addTarget:vc.self action:rightObj forControlEvents:UIControlEventTouchUpInside];
            
            UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
            vc.navigationItem.rightBarButtonItem = rightBtnItem;
        }
        else if ([right isKindOfClass:[NSString class]])
        {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220, 0, 50, 30)];
            UILabel *titleTxt = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 50, 30)];
            titleTxt.text = right;
            [titleTxt setFont:FontSize_Button];
            titleTxt.textColor = NavTitle_TextColor;
            titleTxt.textAlignment = NSTextAlignmentRight;
            [button addSubview:titleTxt];
            [button addTarget:vc.self action:rightObj forControlEvents:UIControlEventTouchUpInside];
            
            [titleTxt setBackgroundColor:[UIColor clearColor]];
            UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
            vc.navigationItem.rightBarButtonItem = rightBtnItem;
        }
    }
    else
        vc.navigationItem.rightBarButtonItems = nil;
    
    if(title != nil){
        UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(110, 8, 100, 25)];
        titleView.text = title;
        [titleView setFont:[UIFont boldSystemFontOfSize:18]];
        [titleView setTextColor:NavTitle_TextColor];
        titleView.textAlignment = NSTextAlignmentCenter;
        [titleView setBackgroundColor:[UIColor clearColor]];
        vc.navigationItem.titleView = titleView;
    }
    
    vc.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
}

void NAV_IMAGEANDTEXT(UIViewController *vc ,NSString *title, NSString *leftTex, UIImage *leftImg ,SEL leftObj, NSString *rightTex, UIImage *rightImg, SEL rightObj)
{
    if(leftTex != nil && leftImg != nil){
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 60)];
        [button setImage:leftImg forState:UIControlStateNormal];
        [button setTitle:leftTex forState:UIControlStateNormal];
        [button addTarget:vc.self action:leftObj forControlEvents:UIControlEventTouchUpInside];
        [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [button setTitleColor:[UIColor colorForHex:@"1296db"] forState:0];
//        button.backgroundColor = [UIColor redColor];
        
        //调整图片和文字上下显示
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
        [button setTitleEdgeInsets:UIEdgeInsetsMake(button.imageView.frame.size.height ,-button.imageView.frame.size.width - 10, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
        [button setImageEdgeInsets:UIEdgeInsetsMake(- button.imageView.frame.size.height+5, -10.0,0.0, -button.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
        
        UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        vc.navigationItem.leftBarButtonItem = leftBarBtnItem;
    }else
         vc.navigationItem.leftBarButtonItem = nil;
    
    
    if(rightTex != nil && rightImg != nil){
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220, 10, 44, 44)];
        [button setImage:rightImg forState:UIControlStateNormal];
        [button setTitle:rightTex forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [button setTitleColor:[UIColor colorForHex:@"1296db"] forState:0];
        [button addTarget:vc.self action:rightObj forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundColor:[UIColor clearColor]];
        
        //调整图片和文字上下显示
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
        [button setTitleEdgeInsets:UIEdgeInsetsMake(button.imageView.frame.size.height ,-button.imageView.frame.size.width + 10, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
        [button setImageEdgeInsets:UIEdgeInsetsMake(-button.imageView.frame.size.height + 5, 10.0,0.0, -button.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
        
        UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        vc.navigationItem.rightBarButtonItem = rightBtnItem;
    }
    else
        vc.navigationItem.rightBarButtonItem = nil;
    
    if(title != nil){
      
        UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(110, 8, 100, 25)];
        titleView.text = title;
        [titleView setFont:[UIFont boldSystemFontOfSize:18]];
        [titleView setTextColor:NavTitle_TextColor];
        titleView.textAlignment = NSTextAlignmentCenter;
        [titleView setBackgroundColor:[UIColor clearColor]];
        vc.navigationItem.titleView = titleView;
    }
}



-(void)initButton:(UIButton*)btn{
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
}

@end
